#!/bin/bash
set -e

# APP_ROOT_PATH="/${NAMESPACE}/app/${APP_NAME}-service"
# CONFIG_FILE_PATH="$APP_ROOT_PATH/conf/config.properties"

# 填充占位符变量
function format()
{
	key="\${$1}";value=$2;filePath=$3;
	echo "未替换   key=$key, value=$value, filePath=$filePath";
	value2=`echo $value | sed 's#\/#\\\/#g'`;
	sed -i 's/'$key'/'$value2'/g' $filePath
	echo "已替换 $key -->  $value2"
}

# 修改IP容器host绑定主机
function host()
{
    HOST_IP=`cat /etc/ip`
    cp /etc/hosts /tmp/hosts
    sed -i '$d' /tmp/hosts
    echo "$HOST_IP  $HOSTNAME" >> /tmp/hosts
    cat /tmp/hosts > /etc/hosts
    cat /etc/hosts
}

# 替换配置文件变量
function replaceVar()
{
    #替换通用参数配置文件
    APP_ROOT_PATH="/usr/local/tomcat/webapps/hcn-specialist"
    CONFIG_HCN_FILE_PATH="$APP_ROOT_PATH/WEB-INF/classes/hcn-specialist.properties"
    CONFIG_SPRING_FILE_PATH="$APP_ROOT_PATH/WEB-INF/classes/spring-cluster-master.xml"

    LOG4J_FILE_PATH="$APP_ROOT_PATH/WEB-INF/classes/log4j2.xml"
    HOST_IP=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v 172.17.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
    format "HOST_IP" "$HOST_IP" $LOG4J_FILE_PATH

    echo "---------- CONFIG_HCN_FILE_PATH -------$CONFIG_HCN_FILE_PATH---"
    # echo "---------- config file $CONFIG_FILE_PATH ----------"
    echo "---------- replace env ----------"
    #替换特定环境变量
    format "OPEN_PARAMS_ENCRYPTION" "$OPEN_PARAMS_ENCRYPTION" $CONFIG_HCN_FILE_PATH
    format "OPEN_HEADER_SIGN_ENCRYPTION" "$OPEN_HEADER_SIGN_ENCRYPTION" $CONFIG_HCN_FILE_PATH

    format "DOMAIN_NAME" "$DOMAIN_NAME" $CONFIG_SPRING_FILE_PATH
    format "DOMAIN_NAME" "$DOMAIN_NAME" $CONFIG_HCN_FILE_PATH
    format "ZK_SVR" "$ZK_SVR" $CONFIG_HCN_FILE_PATH
    format "ZK_ZKAUTH" "$ZK_ZKAUTH" $CONFIG_HCN_FILE_PATH
    format "ZK_ZKAUTH" "$ZK_ZKAUTH" $CONFIG_SPRING_FILE_PATH
    format "DB_SVR" "$DB_SVR" $CONFIG_HCN_FILE_PATH
    format "DB_USER" "$DB_USER" $CONFIG_HCN_FILE_PATH
    format "DB_NAME" "$DB_NAME" $CONFIG_HCN_FILE_PATH
    format "DB_PASS" "$DB_PASS" $CONFIG_HCN_FILE_PATH
    format "DB_PORT" "$DB_PORT" $CONFIG_HCN_FILE_PATH
    format "RPC_SVR" "$RPC_SVR" $CONFIG_HCN_FILE_PATH
    format "BROADCAST_SVR" "$BROADCAST_SVR" $CONFIG_HCN_FILE_PATH
    format "BBP_FILE_PATH" "$BBP_FILE_PATH" $CONFIG_HCN_FILE_PATH
    format "RMQ_NAMESRV" "$RMQ_NAMESRV" $CONFIG_HCN_FILE_PATH
    format "GOL_CLIENT_URL" "$GOL_CLIENT_URL" $CONFIG_HCN_FILE_PATH
    format "HIS_CLIENT_URL" "$HIS_CLIENT_URL" $CONFIG_HCN_FILE_PATH
    format "EDITOR_FOLLOW_UP" "$EDITOR_FOLLOW_UP" $CONFIG_HCN_FILE_PATH
    format "HOL_FAMILY_URL" "$HOL_FAMILY_URL" $CONFIG_HCN_FILE_PATH
    format "HOL_WEB_URL" "$HOL_WEB_URL" $CONFIG_HCN_FILE_PATH
    format "GOL_FAMILY_URL" "$GOL_FAMILY_URL" $CONFIG_HCN_FILE_PATH
    format "IMG_UPLOAD_URL" "$IMG_UPLOAD_URL" $CONFIG_HCN_FILE_PATH
    format "HEALTHRECORD_ORGANIZATION_ID" "$HEALTHRECORD_ORGANIZATION_ID" $CONFIG_HCN_FILE_PATH
    format "ESIGN_URL" "$ESIGN_URL" $CONFIG_HCN_FILE_PATH
    format "SIGN_APP_ID" "$SIGN_APP_ID" $CONFIG_HCN_FILE_PATH
    format "SIGN_APP_SECRET" "$SIGN_APP_SECRET" $CONFIG_HCN_FILE_PATH
    format "WEB_SERVICE_URL" "$WEB_SERVICE_URL" $CONFIG_HCN_FILE_PATH
    format "EXAM_APPOINTMENT_URL" "$EXAM_APPOINTMENT_URL" $CONFIG_HCN_FILE_PATH
    format "WHIS_PATH_URL" "$WHIS_PATH_URL" $CONFIG_HCN_FILE_PATH
    format "COMMON_PROXY_URL" "$COMMON_PROXY_URL" $CONFIG_HCN_FILE_PATH
    format "PHIS_PATH_URL" "$PHIS_PATH_URL" $CONFIG_HCN_FILE_PATH
    format "JYHJ_ORIGINORG" "$JYHJ_ORIGINORG" $CONFIG_HCN_FILE_PATH
    format "JYHJ_URL" "$JYHJ_URL" $CONFIG_HCN_FILE_PATH
    format "COMMON_PROXY_URL" "$COMMON_PROXY_URL" $CONFIG_HCN_FILE_PATH
    format "PROJECT_SOURCE" "$PROJECT_SOURCE" $CONFIG_HCN_FILE_PATH
    format "TENANT_ID" "$TENANT_ID" $CONFIG_HCN_FILE_PATH
    format "IMAGE_URL" "$IMAGE_URL" $CONFIG_HCN_FILE_PATH
    #满意度调查已处理推送消息
    format "SATISFACTION_SURVEY_DIST_URL" "$SATISFACTION_SURVEY_DIST_URL" $CONFIG_HCN_FILE_PATH
    format "WX_MP_APP_ID" "$WX_MP_APP_ID" $CONFIG_HCN_FILE_PATH
    format "WX_MINI_APP_ID" "$WX_MINI_APP_ID" $CONFIG_HCN_FILE_PATH



    echo "---------- replace end ----------end"
}

# 运行Jar服务
function runServer(){
    echo "---------- start server ----------"
    /usr/local/tomcat/bin/catalina.sh run
}
if $HOST_RPC; then
    echo "---------- modify /etc/hosts----------"
     host
fi
 replaceVar
 runServer

